package com.xhs;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import java.awt.Desktop;
import java.net.URI;

/**
 * 小红书登录服务主启动类
 *
 * @author xhs
 */
@SpringBootApplication
@EnableScheduling
public class XhsLoginServiceApplication {

    public static void main(String[] args) {
        // 设置系统属性，禁用AWT头部模式以支持桌面操作
        System.setProperty("java.awt.headless", "false");
        SpringApplication.run(XhsLoginServiceApplication.class, args);
    }

    /**
     * 应用启动完成后自动打开浏览器
     */
    @Component
    public static class BrowserLauncher {

        private final Environment environment;

        public BrowserLauncher(Environment environment) {
            this.environment = environment;
        }

        @EventListener(ApplicationReadyEvent.class)
        public void openBrowser() {
            try {
                // 获取服务器端口
                String port = environment.getProperty("server.port", "8080");
                String url = "http://localhost:" + port + "/xhs/index.html";
                
                // 检查是否支持桌面操作
                if (Desktop.isDesktopSupported()) {
                    Desktop desktop = Desktop.getDesktop();
                    if (desktop.isSupported(Desktop.Action.BROWSE)) {
                        System.out.println("正在自动打开浏览器: " + url);
                        desktop.browse(new URI(url));
                    } else {
                        System.out.println("桌面不支持浏览器操作，请手动打开: " + url);
                    }
                } else {
                    System.out.println("当前环境不支持桌面操作，请手动打开: " + url);
                }
            } catch (Exception e) {
                System.err.println("自动打开浏览器失败: " + e.getMessage());
                String port = environment.getProperty("server.port", "8080");
                System.out.println("请手动打开浏览器访问: http://localhost:" + port + "/xhs/index.html");
            }
        }
    }
} 